package com.example.mobilesafe.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.mobilesafe.domain.ChildInfo;
import com.example.mobilesafe.domain.GroupInfo;

import java.util.ArrayList;
import java.util.List;

public class CommonnumberDao {

    // 1.指定访问数据库的路径
    public static String path = "data/data/com.example.mobilesafe/files/commonnum.db";

    /**
     * 2.开启数据库（组）
     * @return 数据组的集合
     */
    public List<GroupInfo> getGroup(){
        SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
        Cursor cursor = db.query("classlist", new String[]{"name", "idx"}, null, null, null, null, null, null);
        List<GroupInfo> groupInfoList = new ArrayList<>();
        while (cursor.moveToNext()){
            GroupInfo groupInfo = new GroupInfo();
            groupInfo.setName(cursor.getString(0));
            groupInfo.setIndex(cursor.getString(1));
            groupInfo.setChildList(getChild(groupInfo.getIndex()));
            groupInfoList.add(groupInfo);
        }
        cursor.close();
        db.close();
        return groupInfoList;
    }

    /**
     * 3.获取每一个组中孩子节点的数据
     * @param idx 数据组返回的子节点表的id
     */
    public List<ChildInfo> getChild(String idx){
        SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
        Cursor cursor = db.rawQuery("select * from table" + idx + ";", null);
        List<ChildInfo> childInfoList = new ArrayList<>();
        while (cursor.moveToNext()){
            ChildInfo childInfo = new ChildInfo();
            childInfo.set_id(cursor.getString(0));
            childInfo.setNumber(cursor.getString(1));
            childInfo.setName(cursor.getString(2));
            childInfoList.add(childInfo);
        }
        cursor.close();
        db.close();
        return childInfoList;
    }
}